package org.dairy.farms.nandana.web.filters;

import org.dairy.farms.core.NandanaThreadLocal;
import org.dairy.farms.core.UserSession;
import org.dairy.farms.nandana.core.security.userdetails.NandanaGrantedAuthorityImpl;
import org.dairy.farms.nandana.core.security.userdetails.NandanaUserDetails;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import javax.servlet.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: gduggira
 * Date: Jun 24, 2011
 * Time: 4:24:23 PM
 */
public class NandanaThreadLocalFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        try {
            Authentication auth = SecurityContextHolder.getContext().getAuthentication();

            if (auth != null) {
                Object userDetailsObj = auth.getPrincipal();
                if (userDetailsObj != null && (userDetailsObj instanceof UserDetails)) {
                    NandanaUserDetails nandanaUserDetails = (NandanaUserDetails) auth.getPrincipal();
                    UserSession userSession = new UserSession();
                    userSession.setLoginName(nandanaUserDetails.getUsername());
                    userSession.setPersonId(nandanaUserDetails.getPersonId());
                    userSession.setAuthorities(nandanaUserDetails.getAuthorities());
                    NandanaThreadLocal.set(userSession);
                }
            } else {
                //Assuming that we are in test mode.
                List<GrantedAuthority> grantedAuthorityList = new ArrayList<GrantedAuthority>();
                grantedAuthorityList.add(new NandanaGrantedAuthorityImpl("FARM_ADMIN", "Cheryl H. Moody", 4l));
                grantedAuthorityList.add(new NandanaGrantedAuthorityImpl("FARM_DOCTOR", "Cheryl H. Moody", 4l));
                grantedAuthorityList.add(new NandanaGrantedAuthorityImpl("FARM_MANAGER", "Cheryl H. Moody", 4l));
                grantedAuthorityList.add(new NandanaGrantedAuthorityImpl("FARM_WORKER", "Cheryl H. Moody", 4l));
                UserSession userSession = new UserSession();
                userSession.setLoginName("Donec.tempus@euismod.org");
                userSession.setPersonId(2l);
                userSession.setAuthorities(grantedAuthorityList);
                NandanaThreadLocal.set(userSession);
            }
        } finally {
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }

    public void destroy() {
    }
}
